package com.xc.file.web.rest;

import com.xc.core.annotation.Authority;
import com.xc.core.dto.FileDto;
import com.xc.core.enums.EffectStatus;
import com.xc.core.model.TokenModel;
import com.xc.file.bean.FolderFileBean;
import com.xc.file.bean.PackFileBean;
import com.xc.file.bean.QueryFolderBean;
import com.xc.file.bean.QueryShareFolderBean;
import com.xc.file.config.Constants;
import com.xc.file.dto.FolderDto;
import com.xc.file.dto.FolderFileDto;
import com.xc.file.entity.FileEntity;
import com.xc.file.entity.FolderEntity;
import com.xc.file.service.FileService;
import com.xc.file.service.FolderService;
import com.xc.file.service.ShareService;
import com.xc.tool.utils.IpUtils;
import com.xc.tool.utils.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>文件夹及文件rest</p>
 *
 * @author xc
 * @version v1.0.0
 */
@Api(tags = "文件夹及文件rest")
@RestController
public class FolderFileRest {
    @Autowired
    private FolderService folderService;
    @Autowired
    private FileService fileService;
    @Autowired
    private ShareService shareService;
    @Autowired
    private Constants constants;

    @ApiOperation(value = "zip打包文件夹及文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "目录id", name = "folderId", paramType = "path", required = true),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true),
    })
    @PostMapping("/current_user/folder_file/pack")
    @Authority
    public FileDto createCurrentUserFolderFilePack(TokenModel tokenModel, @RequestBody PackFileBean packFileBean) {
        return folderService.packFolderFiles(tokenModel.getUserId(), packFileBean, tokenModel.getAccessToken());
    }

    @ApiOperation(value = "获取共享的文件夹及文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "共享id", name = "shareId", paramType = "path", required = true),
            @ApiImplicitParam(value = "文件夹路径", name = "path", paramType = "query"),
            @ApiImplicitParam(value = "文件夹或文件名称", name = "name", paramType = "query"),
    })
    @GetMapping("/share_folder_files")
    public FolderFileDto getShareFolderFile(HttpServletRequest request, @ModelAttribute QueryShareFolderBean queryShareFolderBean) {
        String clientIp = IpUtils.getClientIp(request);
        return shareService.getShareFolderFile(clientIp, queryShareFolderBean);
    }


    @ApiOperation(value = "获取当前用户的文件夹及文件", notes = "获取当前用户的文件夹及文件信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件夹id", name = "folderId", paramType = "query"),
            @ApiImplicitParam(value = "名称", name = "name", paramType = "query"),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true)
    })
    @GetMapping("/current_user/folder_files")
    @Authority
    public FolderFileDto getCurrentUserFolderFile(TokenModel tokenModel, @ModelAttribute QueryFolderBean queryFolderBean) {
        FolderFileDto folderFileDto = new FolderFileDto();
        List<String> userIds = new ArrayList<>();
        FileEntity fileEntity = new FileEntity();
        FolderEntity folderEntity = new FolderEntity();
        if (!queryFolderBean.getOpenQueryAll()) {
            if (queryFolderBean.getFolderId() == null || "".equals(queryFolderBean.getFolderId())) {
                userIds = tokenModel.getUserIds();
                fileEntity.setFolderId(constants.getRoot());
                folderEntity.setParentNode(constants.getRoot());
            } else {
                FolderEntity folder = folderService.getTargetFolder(tokenModel.getUserId(), queryFolderBean.getFolderId());
                userIds.add(folder.getUserId());
                fileEntity.setFolderId(folder.getId());
                folderEntity.setParentNode(folder.getNode());
            }
        }
        // 查文件
        fileEntity.setName(queryFolderBean.getName());
        fileEntity.setStatus(EffectStatus.VALID.getStatus());
        fileEntity.setUserId(queryFolderBean.getUserId());
        List<FileDto> files = fileService.getUserFileList(tokenModel.getUserId(), userIds, queryFolderBean, fileEntity);
        // 查目录
        folderEntity.setName(queryFolderBean.getName());
        folderEntity.setUserId(queryFolderBean.getUserId());
        List<FolderDto> folders = folderService.getUserFolderList(userIds, queryFolderBean, folderEntity);
        // 路径处理
        if (!queryFolderBean.getOpenQueryAll()) {
            folderFileDto.setFiles(files);
            folderFileDto.setFolders(folders);
        } else {
            for (FileDto fileDto : files) {
                fileDto.setPath(folderService.getPathByFolderId(fileDto.getFolderId()));
            }
            for (FolderDto folderDto : folders) {
                folderDto.setPath(folderService.getPathByFolder(ObjectUtils.convert(new FolderEntity(), folderDto)));
            }
        }
        return folderFileDto;
    }


    @ApiOperation(value = "复制文件夹及文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "新目录id", name = "folderId", paramType = "query", required = true),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true),
    })
    @PostMapping("/current_user/folder_file")
    @Authority
    public FolderFileDto createCurrentUserFolderFile(TokenModel tokenModel, @RequestBody FolderFileBean folderFileBean) {
        return folderService.batchCopyFolderFile(tokenModel.getUserId(), tokenModel.getUserIds(), folderFileBean);
    }

    @ApiOperation(value = "保存共享文件夹及文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "新目录id", name = "folderId", paramType = "query", required = true),
            @ApiImplicitParam(value = "共享id", name = "shareId", paramType = "query"),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true),
    })
    @PostMapping("/current_user/share_folder_file")
    @Authority
    public FolderFileDto createCurrentUserShareFolderFile(TokenModel tokenModel, @RequestParam String shareId, @RequestBody FolderFileBean folderFileBean) {
        return folderService.batchSaveShareFolderFile(tokenModel.getUserId(), shareId, folderFileBean);
    }


    @ApiOperation(value = "移动文件夹及文件")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "目录id", name = "folderId", paramType = "path", required = true),
            @ApiImplicitParam(value = "用户token", name = "token", paramType = "header", required = true),
    })
    @PutMapping("/current_user/folder_file")
    @Authority
    public FolderFileDto updateCurrentUserFolderFile(TokenModel tokenModel, @RequestBody FolderFileBean folderFileBean) {
        return folderService.batchMoveFolderFile(tokenModel.getUserId(), folderFileBean);
    }
}
